package com.asiainfo.fc.net;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;

public class NetHelper {

	private boolean isUserProxy;

	/**
	 * downLoad By Get
	 * 
	 * @param url
	 * @return
	 */
	public String downLoadByGet(String url) {

		try {
			HttpClient client = new HttpClient();
			if (isUserProxy) {
				setProxy(client);
			}
			HttpMethod method = new GetMethod();
			method.setURI(new URI(url, true));
			client.executeMethod(method);
			return method.getResponseBodyAsString();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return null;

	}

	/**
	 * downLoad By Post
	 * 
	 * @param url
	 * @return
	 */
	public String downLoadByPost(String url) {

		try {
			HttpClient client = new HttpClient();
			if (isUserProxy) {
				setProxy(client);
			}

			HttpMethod method = new GetMethod();
			method.setURI(new URI(url, true));
			client.executeMethod(method);
			return method.getResponseBodyAsString();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 
	 * @param host
	 * @param port
	 */
	private void setProxy(HttpClient client) {
		client.getHostConfiguration().setProxy(proxyHost, proxyPort);

	}

	private String proxyHost;
	private int proxyPort;
	private String proxyUserName;

	public String getProxyHost() {
		return proxyHost;
	}

	public void setProxyHost(String proxyHost) {
		this.proxyHost = proxyHost;
	}

	public int getProxyPort() {
		return proxyPort;
	}

	public void setProxyPort(int proxyPort) {
		this.proxyPort = proxyPort;
	}

	public String getProxyUserName() {
		return proxyUserName;
	}

	public void setProxyUserName(String proxyUserName) {
		this.proxyUserName = proxyUserName;
	}

	public String getProxypassWord() {
		return proxypassWord;
	}

	public void setProxypassWord(String proxypassWord) {
		this.proxypassWord = proxypassWord;
	}

	private String proxypassWord;

	public void setUserProxy(boolean isUserProxy) {
		this.isUserProxy = isUserProxy;
	}

	public boolean isUserProxy() {
		return isUserProxy;
	}

}
